package com.tianji.aigc.config;

import com.tianji.aigc.advisor.RecordOptimizationAdvisor;
import com.tianji.aigc.memory.RedisChatMemory;
import com.tianji.aigc.tools.CourseTool;
import com.tianji.aigc.tools.OrderTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringAIConfig {
    /**
     * 1、配置会话记忆方案
     * @return
     */
    @Bean
    public ChatMemory chatMemory(){
        // return new MySQLChatMemory();  // 使用 MySQL 会话记忆方案
        return new RedisChatMemory();  // 使用 Redis 会话记忆方案
    }

    /**
     * 2、配置 ChatClient
     */
    @Bean
    public ChatClient chatClient(ChatClient.Builder chatClientBuilder,
                                 ChatMemory chatMemory,   // 会话记忆
                                 CourseTool courseTool,  // 课程工具
                                 OrderTools orderTools,   // 预下单工具
                                 Advisor recordOptimizationAdvisor) {  // 重复聊天记录处理增强器
        return chatClientBuilder
                .defaultAdvisors(
                        new SimpleLoggerAdvisor(),  //添加 日志记录器Advisor
                        new MessageChatMemoryAdvisor(chatMemory), //添加 会话记忆Advisor
                        recordOptimizationAdvisor  // 添加 重复聊天记录处理增强器
                )
                //.defaultTools(courseTool,orderTools)  // 注册课程工具、预下单工具
                .build();
    }

    /**
     * 自定义 聊天记录 处理的增强器（用于修复 路由工作流智能体 聊天记录重复问题）
     * @param redisChatMemory
     * @return
     */
    @Bean
    public Advisor recordOptimizationAdvisor(RedisChatMemory redisChatMemory){
        return new RecordOptimizationAdvisor(redisChatMemory);
    }

}
